#include "WidgetVariantInt.h"
#include "EVariant.h"
#include "ui_WidgetVariantInt.h"
#include <iostream>

WidgetVariantInt::WidgetVariantInt(QWidget *parent) :
    WidgetVariant(parent),
    ui(new Ui::WidgetVariantInt)
{
    ui->setupUi(this);
}

WidgetVariantInt::~WidgetVariantInt()
{
    delete ui;
}


void WidgetVariantInt::refreshByProperty(EItemMetaPropertyPtr p)
{
    this->clearConnect();

    WidgetVariant::refreshByProperty(p);

    EString text = p->nameCN.isEmpty() ? p->name : p->nameCN;
    ui->labelName->setText( text.data());
    p->getPropertyValue(p->value);
    ui->spinBox->setValue(p->value.to_int());
    ui->spinBox->setReadOnly(p->ifReadOnly);

    ui->spinBox->setEnabled(!p->ifReadOnly);
    this->buildConnect();
}

void WidgetVariantInt::buildConnect()
{
	QObject::connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
}

void WidgetVariantInt::clearConnect()
{
	QObject::disconnect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
}

void WidgetVariantInt::slotValueChanged(int value)
{
    if(m_property != nullptr)
    {
        EVariant varOld = this->m_property->value;
        EVariant var(value);
        m_property->setPropertyValue(var);
        emit signalVariantChanged(this->m_property, varOld);
    }
}
